﻿using MediatR;
using PublicClass;
using S.Services.SheepMale.Domain.Select;
using S.Services.SheepMale.Infrastructure;
using S.Services.SheepMale.Read.Api.Application.ReadCommand.ReadSheepMaleCommand;

namespace S.Services.SheepMale.Read.Api.Application.ReadCommandHandler.ReadSheepMaleCommandHandler
{
    public class GetDigestCommandHandler : IRequestHandler<GetDigestCommand, List<SelectList>>
    {
        private readonly IRepository<Digest> repository;

        public GetDigestCommandHandler(IRepository<Digest> repository)
        {
            this.repository = repository;
        }

        public async Task<List<SelectList>> Handle(GetDigestCommand request, CancellationToken cancellationToken)
        {
            var result = await repository.AllAsync();
            var list = result.ToList();
            List<SelectList> lists = new List<SelectList>();

            foreach (var item in list)
            {
                lists.Add(new SelectList
                {
                    Id = item.DigId,
                    Name = item.DigName
                });
            }
            return lists;
        }
    }
}
